package com.team.sion.service.Impl;

import com.team.sion.mapper.CommentMapper;
import com.team.sion.mapper.ConsumerMapper;
import com.team.sion.mapper.SongListMapper;
import com.team.sion.mapper.SongMapper;
import com.team.sion.pojo.*;
import com.team.sion.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ConsumerMapper consumerMapper;
    @Override
    public void addComment(Comment comment) {
        commentMapper.insert(comment);
    }

    @Override
    public void deleteComment(Integer id) {
        commentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void setSupport(Integer id, Integer up) {
        Comment comment = new Comment();
        comment.setId(id);
        comment.setUp(up);
        commentMapper.updateByPrimaryKeySelective(comment);
    }

    @Override
    public List<Comment> getAllSongListComment(Integer songListId) {
        CommentExample example = new CommentExample();
        example.createCriteria().andSonglistIdEqualTo(songListId).andFatherIdIsNull();
        example.setOrderByClause("up desc");
        //根据歌单id获取一级评论
        List<Comment> comments = commentMapper.selectByExample(example);
        for (Comment c : comments) {
            //封装一级评论用户信息
            c.setConsumer(consumerMapper.selectByPrimaryKey(c.getUserId()));
            //根据fatherId等于一级评论的id获取二级评论 并封装进一级评论
            CommentExample example1 = new CommentExample();
            example1.createCriteria().andFatherIdEqualTo(c.getId());
            c.setSecondComment(commentMapper.selectByExample(example1));
            //处理二级评论
            for (Comment second : c.getSecondComment()) {
                //获取二级评论的用户信息
                second.setConsumer(consumerMapper.selectByPrimaryKey(second.getUserId()));
                //处理更多级评论 直接显示回复某人 仍在二级评论中
                second.setToConsumer(consumerMapper.selectByPrimaryKey(second.getToUserId()));
            }
        }
        return comments;
    }

    @Override
    public List<Comment> getAllSongComment(Integer songId) {
        CommentExample example = new CommentExample();
        example.createCriteria().andSongIdEqualTo(songId);
        return commentMapper.selectByExample(example);
    }
}
